﻿#include "meetpersondialog.h"
#include "ui_meetpersondialog.h"
#include<QString>
#include<QMessageBox>
#include<QDebug>
#include"datachange/sqlconnect.h"
meetPersonDialog::meetPersonDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::meetPersonDialog)
{
    ui->setupUi(this);
    show_per_Info();
    ui->infoTable->horizontalHeader()->setResizeMode(0, QHeaderView::ResizeToContents);
    ui->infoTable->horizontalHeader()->setResizeMode(1, QHeaderView::ResizeToContents);
    ui->infoTable->horizontalHeader()->setResizeMode(2, QHeaderView::Stretch);
    ui->infoTable->horizontalHeader()->setResizeMode(3, QHeaderView::ResizeToContents);
    ui->infoTable->horizontalHeader()->setResizeMode(4, QHeaderView::ResizeToContents);
    //设置table控件状态
    ui->infoTable->setSelectionBehavior ( QAbstractItemView::SelectRows); //设置选择行为，以行为单位
    ui->infoTable->setSelectionMode ( QAbstractItemView::SingleSelection); //设置选择模式，选择单行
    ui->infoTable->setEditTriggers(QAbstractItemView::NoEditTriggers);

    connect(ui->search_but,SIGNAL(clicked(bool)),this,SLOT(on_buttonFind_clicked()));
    connect(ui->infoTable,SIGNAL(cellDoubleClicked(int,int)),this,SLOT(close()));//表格双击事件
    connect(ui->infoTable,SIGNAL(cellDoubleClicked(int,int)),this,SLOT(on_two_click_table(int)));//表格双击事件
    connect(ui->infoTable,SIGNAL(cellDoubleClicked(int,int)),parent,SLOT(showMeetPer()));//表格双击事件


}

meetPersonDialog::~meetPersonDialog()
{
    delete ui;
}

void meetPersonDialog::show_per_Info(){
    //sqlconnect(db);
    ui->infoTable->setRowCount(0);
    QSqlQuery query;
    bool ok=query.exec("select CadreName,Birth,IDNum,Sex from CadreInfo");
    //qDebug()<<ok;
    while(query.next())
    {
    int rowc=ui->infoTable->rowCount();//获取行数
    ui->infoTable->insertRow(rowc);
    //干部姓名
    QString a=query.value(0).toString();
    ui->infoTable->setItem(rowc,0,new QTableWidgetItem(a));

    //出生日期
    QString a2=query.value(1).toString();
    ui->infoTable->setItem(rowc,1,new QTableWidgetItem(a2));
    //性别
    QString a4=query.value(3).toString();
    if(a4==QString::fromLocal8Bit("1"))
    ui->infoTable->setItem(rowc,3,new QTableWidgetItem("男"));
    if(a4==QString::fromLocal8Bit("0"))
    ui->infoTable->setItem(rowc,3,new QTableWidgetItem("女"));

    //证件号码
    QString a3=query.value(2).toString();
    ui->infoTable->setItem(rowc,2,new QTableWidgetItem(a3));

    QString sql2="select PostName from Post where IDNum='"+a3+"' and QualifyType='0'";
    QSqlQuery query2;
    bool ok2=query2.exec(sql2);
    //qDebug()<<ok2;
    while(query2.next()){
        //现任职务
        QString m=query2.value(0).toString();
        ui->infoTable->setItem(rowc,4,new QTableWidgetItem(m));
    }
    }
    //db.close();
}

void meetPersonDialog::on_two_click_table(int row){
    //sqlconnect(db);
    QTableWidgetItem *meetPerID = new QTableWidgetItem;
    meetPerID = ui->infoTable->item(row,2);
    QString numid=meetPerID->text();
    QSqlQuery query;
    QString sql="SELECT PeopleID FROM `ZZB`.`MeetingPeople` where MeetingID='"+meetid+"' and IDNum='"+numid+"'";
    bool ok1=query.exec(sql);
    query.next();
    QString peopleidGeted=query.value(0).toString();
    //qDebug()<<ok1<<"   "<<peopleidGeted;
    //如果名字为空，则可插入
    if(peopleidGeted.count()==0){
        QSqlQuery query2;
        QString sql2="INSERT INTO MeetingPeople(MeetingID,IDNum)VALUES('"+meetid+"','"+numid+"')";
        bool ok=query2.exec(sql2);
        //bool ok2=query.first();
        //qDebug()<<"双击错";
        if(ok)
        {
                //ui->status->setText("添加信息状态:添加成功");
                qDebug()<<"s";

        }
        else
        {
                //ui->status->setText("添加信息状态:添加失败");
                qDebug()<<"eeeeee"<<this->db.lastError();
        }
    }
    else{
        QMessageBox::information(this,"提示信息","此人已添加!");
        return;
    }
//this->db.close();
}

//上方干部信息行查询
void meetPersonDialog::on_buttonFind_clicked()
{
    int rows = ui->infoTable->rowCount();
    QString gbName = ui->nameEdit->text();
    //qDebug()<< gbName;
    if ("" == gbName)
    {
        show_per_Info();
    }
    else
    {
        QList <QTableWidgetItem *> item = ui->infoTable->findItems(gbName, Qt::MatchContains);

        for (int i = 0; i < rows; i++)
        {
            ui->infoTable->setRowHidden(i, true);//隐藏所有行
        }

        if (!item.isEmpty())
        {
            for (int i = 0; i < item.count(); i++)
            {
                ui->infoTable->setRowHidden(item.at(i)->row(),false);//item.at(i).row()输出行号
            }

            //QString newtext = QString(" 共: %1 条").arg(item.count());
            //ui->label_4->setText(newtext);
        }
    }
}
void meetPersonDialog::reciveMeetID(QString id){
    meetid=id;

}
